package many2one;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;


import util.HibernateUtil;

public class TestMany2One {
	//保存员工和部门的保存
	//部门尚不存在
	@Test
	public void testSave1(){
		Session s =HibernateUtil.getSession();
		Transaction t=s.beginTransaction();
		Dept dept = new Dept();
		dept.setLocation("溆浦");
		dept.setName("sales");
		Emp emp=new Emp();
		emp.setDept(dept);
		emp.setName("heantai");
		s.save(emp);
		t.commit();
	}
	//保存员工和部门的保存
	//部门已存在
	@Test
	public void testSave2()
	{
		Session session = HibernateUtil.getSession();
		Transaction t = session.beginTransaction();
		
		Dept dept = (Dept)session.load(Dept.class, 1);
		Emp emp = new Emp();
		emp.setName("emp1");
		
		emp.setDept(dept);
		
		
		//对emp进行保存
		session.save(emp);
		t.commit();
		
	}
	//读取emp，同时从emp中获取dept
	@Test
	public void testRead(){
		Session s =HibernateUtil.getSession();
		Transaction t=s.beginTransaction();
//		Emp emp=(Emp) s.get(Emp.class, 4);
		Emp emp=(Emp) s.load(Emp.class, 4);//看使用与不使用fetch="join"的区别
		//不打印，两者的区别
//		System.out.println(emp.getName());
		System.out.println(emp.getName()+";"+emp.getDept().getName());
		t.commit();
	}
	//通过Hql读取emp及dept
	@Test
	public void testHql1()
	{
		Session s = HibernateUtil.getSession();
		Transaction t=s.beginTransaction();
		Query query = s.createQuery("from Emp");
		List<Emp> emps = query.list();
		for(Emp emp : emps){
			System.out.println(emp.getName()+","+emp.getDept().getName());
		}
		t.commit();
	}
	
	
	//通过Hql读取emp及dept——join查询
	@Test
	public void testHql2(){
		Session s = HibernateUtil.getSession();
		Transaction t = s.beginTransaction();
		Query query = s.createQuery("from Emp e join fetch e.dept");
		List<Emp> emps = query.list();
		for(Emp emp : emps){
			System.out.println(emp.getName()+","+emp.getDept().getName());
		}
		t.commit();
	}
	
	//通过Hql读取emp及dept——员工姓名作为条件
	@Test
	public void testHql3()
	{
		Session s = HibernateUtil.getSession();
		Transaction t = s.beginTransaction();
		Query query = s.createQuery("from Emp e join fetch e.dept where e.name like ?");
		query.setParameter(0, "emp%");
		List<Emp> emps = query.list();
		for(Emp emp : emps){
			System.out.println(emp.getName()+","+emp.getDept().getName());
		}
		t.commit();
		
		
	}
	
	/**
	 * 通过Hql读取emp及dept——部门名称作为条件
	 */
	@Test
	public void testHql4(){
		Session s = HibernateUtil.getSession();
		Transaction t =s.beginTransaction();
		Query query=s.createQuery("from Emp e where e.dept.name=?");
		query.setParameter(0, "sales");
		Iterator<Emp> emps=query.iterate();
		while (emps.hasNext()) {
			Emp emp = (Emp) emps.next();
			System.out.println(emp);
		}
		t.commit();
		
	}
	
	/**
	 * //通过Hql读取Dept——员工姓名作为条件
	 */
	@Test
	public void testHql5(){
		Session s = HibernateUtil.getSession();
		Transaction t =s.beginTransaction();
		Query query=s.createQuery("select distinct e.dept from Emp e where e.name=?");
		query.setParameter(0, "mj");
		Iterator<Emp> emps=query.iterate();
		while (emps.hasNext()) {
			Emp emp = (Emp) emps.next();
			System.out.println(emp);
		}
		t.commit();
	}
}
